
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users

from time import strftime, localtime
from datetime import timedelta, date, datetime
import calendar

import random,re
import os

import urllib,urllib2
from xml.sax.saxutils import escape

from models.video import Video
from models.article import Article

_DEBUG=True

class Methods():
  namelist = [u'anime',u'comic',u'game']
  @staticmethod
  def delete():
      for element in elementList:
          element.delete();

  @staticmethod
  def today():
    year = strftime("%Y",localtime())
    month  = strftime("%m",localtime())
    day  = strftime("%d",localtime())
    return (year + "-" + month + "-" + day)

class BaseHandler(webapp.RequestHandler):
  def render(self, template_name, template_values={}):

    user = users.get_current_user()

    if user:
      log_in_out_url = users.create_logout_url(self.request.uri)
    else:
      log_in_out_url = users.create_login_url(self.request.path)

    values = {'user': user,
              'items':Methods.namelist,
              'log_in_out_url': log_in_out_url}
    values.update(template_values)

    directory = os.path.dirname(__file__)
    path = os.path.join(directory, 'templates', template_name)

    self.response.out.write(template.render(path, values, debug=_DEBUG))

class Index(BaseHandler):
    def get(self):
        articles=Article.all()

        articles_string = []

        for article in articles:
            a.keyname = a.key().name()
            articles_string.append(a) 
        template_values = {'articles_string':articles_string}
        self.render('articles.html',template_values)

class VideoPlayer(BaseHandler):
  def get(self):
    articles=Article.all()

    articles_string = []

    for article in articles:
      a.keyname = a.key().name()
      articles_string.append(a) 
      template_values = {'articles_string':articles_string}
      self.render('articles.html',template_values)

class Notice(BaseHandler):
  def get(self):
    date = Methods.today();
    template_values = {'date':date}
    self.render('notice.html',template_values)

class PostNotice(BaseHandler):
  def get(self):
    notice=Notice()

    notice.date= self.request.get('date')
    notice.title=self.request.get('title')
    notice.urllink=self.request.get('urllink')

    notice.put()
    self.redirect('/')


def main():
    application = webapp.WSGIApplication([('/', Index),
                                          ('/Notice', Notice),
                                          ('/PostNotice', PostNotice),
                                          (r'/videos/(.*)/(.*)', VideoPlayer)],
                                         
                                         debug=True)
    
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
